# 语法： with expression [as variable]

# 最常用法 文件操作
with open('data.txt') as f:
    data = f.read()
    print(data)

with open('input.txt', 'r') as source, open('output.txt', 'w') as target:
    content = source.read()
    target.write(content.upper())

# 定义一个类，支持with 语句
class MyResource:
    def __init__(self, name):
        self.name = name
    def __enter__(self):
        print("Entering the context")
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        print("Exiting the context")
        if exc_type:
            print(f"An exception occurred: {exc_value}")
        return True  # Suppress exception if any

    def do_something(self):
        print("Doing something with the resource")


with MyResource('my_resource') as resource:
    resource.do_something()



def test():
    print("test function     start")
    pass #这里不执行任何操作，不影响下面的执行
    print("test function     end")

test()